package com.spirng.leetcode.day06;

import java.util.*;

public class Test107 {
    public List<List<Integer>> levelOrderBottom(TreeNode root) {
        Queue<TreeNode> queue=new LinkedList<TreeNode>();
        ArrayList<List<Integer>> lists = new ArrayList<>();
        if(root==null){
            return lists;
        }
        queue.offer(root);
        while (!queue.isEmpty()){
            int size=queue.size();
            ArrayList<Integer> list = new ArrayList<>();
            for (int i = 0; i < size; i++) {
                TreeNode node = queue.remove();
                if(node!=null){
                    list.add(node.val);
                }
                if(node.left!=null){
                    queue.add(node.left);
                }
                if(node.right!=null){
                    queue.add(node.right);
                }
            }
            lists.add(list);
        }
        Collections.reverse(lists);
        return lists;
    }
}
